package com.erp.web.controller.system;

import com.erp.framework.annotation.Log;
import com.erp.framework.constant.ConfigConstants;
import com.erp.framework.core.controller.BaseController;
import com.erp.framework.core.domain.AjaxResult;
import com.erp.framework.core.page.TableDataInfo;
import com.erp.framework.core.text.Convert;
import com.erp.framework.enums.BusinessType;
import com.erp.framework.util.AutoFillUtils;
import com.erp.framework.util.StringUtils;
import com.erp.framework.util.poi.ExcelUtil;
import com.erp.system.domain.Material;
import com.erp.system.domain.MaterialReplace;
import com.erp.system.domain.MaterialVendor;
import com.erp.system.domain.Vendor;
import com.erp.system.service.MaterialReplaceService;
import com.erp.system.service.MaterialService;
import com.erp.system.service.MaterialVendorService;
import com.erp.system.service.VendorService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * FileName: MaterialController.java
 * SpringMVC控制器的详细说明
 *
 * @version 1.00
 * @Date 创建日期
 */

@Controller
@RequestMapping("/system/material")
public class MaterialController extends BaseController {

    @Autowired
    private MaterialService materialService;

    @Autowired
    private VendorService vendorService;

    @Autowired
    private MaterialVendorService materialVendorService;

    @Autowired
    private MaterialReplaceService materialReplaceService;

    private final String prefix = "system/material";

    @RequiresPermissions("system:material:view")
    @GetMapping()
    public String config() {
        return prefix + "/material";
    }

    @RequiresPermissions("system:material:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Material material) {
        startPage();
        List<Material> list = materialService.queryAll(material);
        return getDataTable(list);
    }

    /**
     * 新增物料
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存物料
     */
    @RequiresPermissions("system:material:add")
    @Log(title = "新增物料", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Material material) {
        if (materialService.checkCodeUnique(material)) {
            return error("物料编码已存在，请检查！");
        }
        AutoFillUtils.setCreateInfo(material);
        return toAjax(materialService.insertMaterial(material));
    }

    /**
     * 修改物料跳转页面
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        mmap.put("material", materialService.queryById(id));
        return prefix + "/edit";
    }

    /**
     * 更新保存物料
     */
    @RequiresPermissions("system:material:edit")
    @Log(title = "更新物料主数据", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Material material) {
        if (materialService.checkCodeUnique(material)) {
            return error("物料编码已存在，请检查！");
        }
        AutoFillUtils.setEditInfo(material);
        return toAjax(materialService.updateMaterial(material));
    }

    @RequiresPermissions("system:material:remove")
    @Log(title = "删除物料", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        String[] id = Convert.toStrArray(ids);
        return toAjax(materialService.deleteMaterialByIds(id));
    }

    @RequiresPermissions("system:material:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Material material, String id) {
        List<Material> list;
        //根据筛选框导出，否则根据勾选导出
        if (StringUtils.isEmpty(id)) {
            list = materialService.queryAll(material);
        } else {
            String[] ids = Convert.toStrArray(id);
            list = materialService.queryByIds(ids);
        }
        ExcelUtil<Material> util = new ExcelUtil<>(Material.class);
        return util.exportExcel(list, "物料主数据");
    }

    /**
     * 校验编码
     */
    @PostMapping("/checkCodeUnique")
    @ResponseBody
    public boolean checkCodeUnique(Material material) {
        return StringUtils.isNotBlank(material.getCode()) && materialService.checkCodeUnique(material);
    }

    /**
     * 获取已指定的供应商列表
     */
    @RequiresPermissions("system:material:viewMaterialVendor")
    @GetMapping("/viewMaterialVendor/{materialId}")
    public String getMaterialVendorList(@PathVariable("materialId") String materialId, ModelMap mmap) {
        MaterialVendor materialVendor = new MaterialVendor();
        materialVendor.setMaterialId(materialId);
        mmap.put("materialVendor",materialVendor);
        mmap.put("material",materialService.queryById(materialId));
        return prefix + "/viewMaterialVendor";
    }

    /**
     * 获取已指定的供应商列表
     */
    @RequiresPermissions("system:material:viewMaterialVendor")
    @PostMapping("/viewMaterialVendor")
    @ResponseBody
    public TableDataInfo getMaterialVendorList(MaterialVendor materialVendor) {
        startPage();
        List<MaterialVendor> list =  materialVendorService.queryAll(materialVendor);
        return getDataTable(list);
    }
    /**
     * 跳转新增页面
     */
    @RequiresPermissions("system:material:addMaterialVendor")
    @GetMapping("/addMaterialVendor/{id}")
    public String addMaterialVendor(@PathVariable("id")String id, ModelMap mmap) {
        Vendor vendor = new Vendor();
        vendor.setStatus(ConfigConstants.ENABLE);
        mmap.put("vendorList", vendorService.queryAll(vendor));
        mmap.put("material", materialService.queryById(id));
        return prefix + "/addMaterialVendor";
    }

    /**
     * 新增指定供应商
     */
    @RequiresPermissions("system:material:addMaterialVendor")
    @PostMapping("/addMaterialVendorSave")
    @ResponseBody
    public AjaxResult addMaterialVendorSave(MaterialVendor materialVendor) {
        MaterialVendor param = new MaterialVendor();
        param.setMaterialId(materialVendor.getMaterialId());
        List<MaterialVendor> list = materialVendorService.queryAll(param);
        for (MaterialVendor obj : list) {
            if (obj.getVendorId().equals(materialVendor.getVendorId())) {
                return error("存在维护记录，请执行修改");
            }
            if (obj.getType().contains(ConfigConstants.MAIN)) {
                return error("主供应商已维护，请先变更供应商[" + obj.getVendor().getCode() + "]");
            }
        }
        AutoFillUtils.setCreateInfo(materialVendor);
        return toAjax(materialVendorService.insert(materialVendor));
    }

    /**
     * 跳转修改页面
     */
    @RequiresPermissions("system:material:editMaterialVendorSave")
    @GetMapping("/editMaterialVendor/{id}")
    public String editMaterialVendor(@PathVariable("id") String id, ModelMap mmap) {
        mmap.put("materialVendor", materialVendorService.queryById(id));
        return prefix + "/editMaterialVendor";
    }

    /**
     * 修改指定供应商
     */
    @RequiresPermissions("system:material:editMaterialVendorSave")
    @PostMapping("/editMaterialVendorSave")
    @ResponseBody
    public AjaxResult editMaterialVendorSave(MaterialVendor materialVendor) {
        AutoFillUtils.setEditInfo(materialVendor);
        return toAjax(materialVendorService.update(materialVendor));
    }

    /**
     * 冻结指定供应商
     */
    @RequiresPermissions("system:material:removeMaterialVendorSave")
    @PostMapping("/removeMaterialVendorSave")
    @ResponseBody
    public AjaxResult removeMaterialVendorSave(String[] ids) {
        return toAjax(materialVendorService.delete(ids));
    }

    /**
     * 获取维护的替换料列表
     */
    @RequiresPermissions("system:material:viewMaterialReplace")
    @GetMapping("/viewMaterialReplace/{materialId}")
    public String viewMaterialReplace(@PathVariable("materialId") String materialId, ModelMap mmap) {
        MaterialReplace materialReplace = new MaterialReplace();
        materialReplace.setRawMaterialId(materialId);
        mmap.put("materialReplace",materialReplace);
        mmap.put("material",materialService.queryById(materialId));
        return prefix + "/viewMaterialReplace";
    }

    /**
     * 获取已指定的供应商列表
     */
    @RequiresPermissions("system:material:viewMaterialReplace")
    @PostMapping("/viewMaterialReplace")
    @ResponseBody
    public TableDataInfo getViewMaterialReplace(MaterialReplace materialReplace) {
        startPage();
        List<MaterialReplace> list =  materialReplaceService.queryAll(materialReplace);
        return getDataTable(list);
    }

    /**
     * 跳转新增页面
     */
    @RequiresPermissions("system:material:addMaterialReplaceSave")
    @GetMapping("/addMaterialReplaceSave/{id}")
    public String addMaterialReplace(@PathVariable("id") String id , ModelMap mmap) {
        // 被指定的物料
        Material rawMaterial = materialService.queryById(id);
        MaterialReplace materialReplace = new MaterialReplace();
        materialReplace.setRawMaterial(rawMaterial);
        Material param = new Material();
        param.setStatus(ConfigConstants.ENABLE);
        mmap.put("materialReplace",materialReplace);
        mmap.put("materialList", materialService.queryAll(param));
        return prefix + "/addMaterialReplace";
    }

    /**
     * 新增替换料
     */
    @RequiresPermissions("system:material:addMaterialReplaceSave")
    @PostMapping("/addMaterialReplaceSave")
    @ResponseBody
    public AjaxResult addMaterialReplaceSave(MaterialReplace materialReplace) {
        MaterialReplace param = new MaterialReplace();
        param.setRawMaterialId(materialReplace.getRawMaterialId());
        List<MaterialReplace> list = materialReplaceService.queryAll(param);
        for (MaterialReplace obj : list) {
            if (obj.getNewMaterialId().equals(materialReplace.getNewMaterialId())) {
                return error("存在维护记录，请执行修改");
            }
//            if (obj.getType().contains(ConfigConstants.MAIN)) {
//                return error("主替换料已维护，请先变更替换料[" + obj.getNewMaterial().getCode() + "]");
//            }
        }
        AutoFillUtils.setCreateInfo(materialReplace);
        return toAjax(materialReplaceService.insert(materialReplace));
    }

    /**
     * 跳转修改页面
     */
    @RequiresPermissions("system:material:editMaterialReplaceSave")
    @GetMapping("/editMaterialReplaceSave/{id}")
    public String editMaterialReplace(@PathVariable("id") String id, ModelMap mmap) {
        Material param = new Material();
        param.setStatus(ConfigConstants.ENABLE);
        mmap.put("materialList", materialService.queryAll(param));
        mmap.put("materialReplace", materialReplaceService.queryById(id));
        return prefix + "/editMaterialReplace";
    }

    /**
     * 修改指定替换料
     */
    @RequiresPermissions("system:material:editMaterialReplaceSave")
    @PostMapping("/editMaterialReplaceSave")
    @ResponseBody
    public AjaxResult editMaterialReplaceSave(MaterialReplace materialReplace) {
        AutoFillUtils.setEditInfo(materialReplace);
        return toAjax(materialReplaceService.update(materialReplace));
    }

    /**
     * 冻结指定替换料
     */
    @RequiresPermissions("system:material:removeMaterialReplaceSave")
    @PostMapping("/removeMaterialReplaceSave")
    @ResponseBody
    public AjaxResult removeMaterialReplaceSave(String[] ids) {
        return toAjax(materialReplaceService.delete(ids));
    }

}
